﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MarkControle.Models
{
    public class Funcionario:IComparable<Funcionario>
    {
       
        private string nome;
        private string username;
        private string senha;
        private string cpf;
        private int bloq;
        int trfcumpridas = 0;
        int trf = 0;
        int trfadiadas = 0;
        int trfatrasadas = 0;
        private int acesso;
        private string area;
        private string Senmax;
        public string master = "ipcamp";
        
        
        
        
        
        
        
        [Required]
        public string Senmax1
        {
            get { return Senmax; }
            set { Senmax = value; }
        }

       
        [Range(0,2)]
        public int Acesso
        {
            get { return acesso; }
            set { acesso = value; }
        }

        public int Trfadiadas
        {
            get { return trfadiadas; }
            set { trfadiadas = value; }
        }


        public int Trfatrasadas
        {
            get { return trfatrasadas; }
            set { trfatrasadas = value; }
        }
        public int Trf
        {
            get { return trf; }
            set { trf = value; }
        }
        public int Trfcumpridas
        {
            get { return trfcumpridas; }
            set { trfcumpridas = value; }
        }
        [Required]
        public string Area
        {
            get { return area; }
            set { area = value; }
        }
        public int Bloq
        {
            get { return bloq; }
            set { bloq = value; }
        }
        [Required]
        public string Nome
        {
            get { return nome; }
            set { nome = value; }
        }
        [Required]
        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        [Required]
        public string Senha
        {
            get { return senha; }
            set { senha = value; }
        }
        [Required]
        public string Cpf
        {
            get { return cpf; }
            set { cpf = value; }
        }


  


        public Funcionario()
        {
        }

        public Funcionario(string nome,string username,string senha,string cpf,int bloq,int acess,string area)
        {
            this.nome = nome;
            this.username = username;
            this.senha = senha;
            this.cpf = cpf;
            this.bloq = bloq;
            this.acesso = acess;
            this.area = area;
           

        }
        public void Adicionabanco()
        {
            string urlConn = "Database=markcontrole;Data Source=mysql01.markcontrole.hospedagemdesites.ws;User Id=markcontrole;Password=ivan120869; pooling=false";

            // Create objeto Connection
            MySqlConnection conn = new MySqlConnection(urlConn);

            // Open Connection
            try
            {
                conn.Open();
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error connection by: {0}", e.Message);
            } // end try

            // Validate connection
            if (conn.State.Equals(ConnectionState.Open))
            {
                MySqlTransaction transaction = null;
                try
                {
                    // Open transaction

                    transaction = conn.BeginTransaction();

                    // Create query e Command
                    StringBuilder sql = new StringBuilder();
                    sql.Append("INSERT INTO funcionario(CPF,NOME,USERNAME,SENHA,BLOQ,ACESSO,AREA,TAREFASCUMPRIDAS,TAREFASADIADAS,TAREFASATRASADAS,TOTTAREFA)");
                    sql.Append("VALUES(@cpf, @nom,@user,@sen,@bloq,@acss,@area,@tarcum,@taradi,@taratr,@tartot)");
                    MySqlCommand command = new MySqlCommand(sql.ToString(), conn);

                    // Add Parameters
                    command.Parameters.AddWithValue("@cpf", cpf);
                    command.Parameters.AddWithValue("@nom", nome);
                    command.Parameters.AddWithValue("@user", username);
                    command.Parameters.AddWithValue("@sen", senha);                  
                    command.Parameters.AddWithValue("@bloq", bloq);
                    command.Parameters.AddWithValue("@acss", acesso);
                    command.Parameters.AddWithValue("@area", area);
                    command.Parameters.AddWithValue("@tarcum", trfcumpridas);
                    command.Parameters.AddWithValue("@taradi", trfadiadas);
                    command.Parameters.AddWithValue("@taratr", trfatrasadas);
                    command.Parameters.AddWithValue("@tartot", trf);
                  


                    

                    // Insert customer
                    command.ExecuteNonQuery();


                    // Commit transaction
                    transaction.Commit();
                }
                catch (MySqlException e)
                {
                    transaction.Rollback();
                    
                }
                finally
                {
                    // Close connection
                    conn.Close();
                } // end try
            }
           
        }

        public void Edita(string id,string nsenha)
        {
            if (id == senha || id == master)
            {
                senha = nsenha;
            }
        }

       
        public void Deletebanco(string nome1,string username1)
        {



            string urlConn = "Database=markcontrole;Data Source=mysql01.markcontrole.hospedagemdesites.ws;User Id=markcontrole;Password=ivan120869; pooling=false";
            // Create objeto Connection
            MySqlConnection conn = new MySqlConnection(urlConn);

            // Open Connection
            try
            {
                conn.Open();
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error connection by: {0}", e.Message);
            } // end try

            // Validate connection
            if (conn.State.Equals(ConnectionState.Open))
            {
                MySqlTransaction transaction = null;
                try
                {
                    // Open transaction
                    transaction = conn.BeginTransaction();

                    // Create query e Command
                    StringBuilder sql = new StringBuilder();
                    sql.Append("DELETE FROM funcionario WHERE NOME=@nom AND USERNAME=@user");
                    MySqlCommand command = new MySqlCommand(sql.ToString(), conn);

                    // Add Parameters
                    command.Parameters.AddWithValue("@nom", nome1);
                    command.Parameters.AddWithValue("@user", username1);
                    

                    // Insert customer
                    command.ExecuteNonQuery();


                    // Commit transaction
                    transaction.Commit();
                }
                catch (MySqlException e)
                {
                    transaction.Rollback();
                    
                }
                finally
                {
                    // Close connection
                    conn.Close();
                } // end try
            }




        }

        public int CompareTo(Funcionario FuncionariotoCompare)
        {
            if (String.Compare(this.Area, FuncionariotoCompare.Area) == 1)
                return 1;
            else if (String.Compare(this.Area, FuncionariotoCompare.Area) == -1)
                return -1;
            else
                return String.Compare(FuncionariotoCompare.Nome,this.Nome);
        }

        


       
    }


}